/*
###############################################################################################
#                                                                                             # 
# The Page is used for Add a new admin user from the Admin Control Panel. This Page is        #
# created inside the Admin Control Panel and only the Administrator has a right to Add        #
# the new Site Users.                                                                         #
# The data will be used from the tbl_Admin table                                              #
#     Author : Sharvan Kumar                                                                 #
#     Dated  : 16-Feb-2006                                                                    #
#                                                                                             #
###############################################################################################
*/


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;


public partial class Admin_Ad_User : System.Web.UI.Page
{// creating an instance of manager class
    Manager manager = new Manager(true,"ConnectionString");
    int userID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // check page mode
            if (Request.QueryString.HasKeys())
            {
                if (Request.QueryString["uid"] != null && Request.QueryString["uid"].ToString().Trim() != "")
                {
                    try
                    {
                        userID = Convert.ToInt32(Request.QueryString["uid"].ToString());
                        Button1.Text = "Update User";
                        //check user id
                        if (Session["ADMIN_ID"].ToString() == "1")
                        {   
                            txtUserName.Enabled = false;
                        }
                        //submit.Visible=false; 						
                    }
                    catch 
                    {
                        Response.Write("User Id Is Invalid");
                        Response.End();
                    }
                    FillValue(userID);

                }
            }
        }
    }
    //fill value in form in case of update
    private void FillValue(int userid)
    {
        SqlDataReader dr = manager.ExecuteQuery("select * from tbl_admin where adminid='" + userid + "'");
        if (dr.Read())
        {
            txtUserName.Text = dr["username"].ToString();
            txtFirstName.Text = dr["firstname"].ToString();
            txtLastName.Text = dr["lastname"].ToString();
            txtPassword.Text = dr["password"].ToString();
            txtConPassword.Text = dr["password"].ToString();
            
            lblMessage.Text = "update user";
            lblMain.Text = "Update User";
            uid.Text = userid.ToString();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string struser = txtUserName.Text.Replace("'", "''").Trim();
        string strpass = txtPassword.Text.Replace("'", "''").Trim();
        string strFname = txtFirstName.Text.Replace("'", "''").Trim();
        string strLname = txtLastName.Text.Replace("'", "''").Trim();
        
        //Add new User
        if (!(uid.Text.Trim().Length > 0))
        {
            string strins = "insert into tbl_Admin (username,password,firstname,lastname) values('";
            strins +=  struser + "','" + strpass + "','" + strFname + "','" + strLname + "')";
            //check user name 
            //if (!((manager.ExecuteQuery("select adminid from tbl_admin where username='" + struser + "'")).Read()))

            string strUserExists = "select count(adminid) from tbl_admin where username='" + struser + "'";
            object objReturn = manager.ExecuteScalarQuery(strUserExists);
            if (!Equals(objReturn, null))
            {
                string usernameCount = Convert.ToString(objReturn);
                if (Equals(usernameCount, "0"))
                {
                    manager.ExecuteUpdate(strins);
                    lblMessage.Text = "User Added";
                    lblMessage.Visible = true;
                    clearfields();
                }
                else
                {
                    lblMessage.Text = "User name already exists.";
                    lblMessage.Visible = true;
                }
            }
        }
        //update user
        else
        {
            string strq = "select adminid from tbl_admin where username='" + struser + "' and adminid<>'" + uid.Text + "'";
            //Response.Write(strq);
            //Response.End();  
            //check user for existince
            SqlDataReader dr;
            dr = manager.ExecuteQuery(strq);
            //Response.Write(strq);
            //if (!((manager.ExecuteQuery(strq)).Read()))
            if (!dr.Read())
            {
                dr.Close();
                string strup = "update tbl_admin set username='" + struser + "'";
                strup += ",password='" + strpass + "',firstname='" + strFname + "',lastname='" + strLname + "'";
                strup += " where adminid='" + uid.Text + "'";
                manager.ExecuteUpdate(strup);
                lblMessage.Text = "User Updated";
                lblMessage.Visible = true;
            }
            else
            {
                dr.Close();
                lblMessage.Text = "User name already exists.";
                lblMessage.Visible = true;
            }
        }
    }

    private void clearfields()
    {
        Control myForm = Page.FindControl("form1");
        foreach (Control ctl in myForm.Controls)
        {	//to clear all Text Box
            if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
                ((TextBox)ctl).Text = "";
        }
    }

}
